$(function () {
    var jqdata;//=[{"userid":"01","username":"皮皮虾","password":"动态biubiubiu"},{"userid":"02","username":"象拔蚌","password":"动态boomboomboom"}];
    var names=[];
    var model=[];

    $.get(baseURL + "water/tdataerr/status", function(r){
        jqdata = r;
        //JSON.parse
        $.each(jqdata,function(idx,obj){
            /*for(var p in jqdata[idx]){//遍历json对象的每个key/value对,p为key
                //console.log(p + "-" + jqdata[idx][p]);
            }*/
            if (idx == 0){
                for(var p in obj){//遍历json对象的每个key/value对,p为key
                    names.push(p);
                    model.push({
                        name:p,
                        //index:p,
                        width:100,
                        sortable:false
                    });
                }
            }
        });

        $("#jqGrid").jqGrid({
            url: baseURL + 'water/tdataerr/status',
            datatype: "json",
            colNames : names,
            colModel : model,
            viewrecords: true,
            height:$(window).height()-130,
            rowNum: jqdata.length,
            rowList : [10,30,50],
            rownumbers: true,
            rownumWidth: 50,
            autowidth:true,
            multiselect: true,
            footerrow: true,
            //pager: "#jqGridPager",
            jsonReader : {
                root: "page.page",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });

                //计算汇总行
                //var rows = $("#jqGrid").jqGrid("getRowData"), totalFooter = [];
                //for(var i = 0, l = rows.length; i<l; i++) {
                //    totalFooter
                //}

                var footer="{";
                for(var i = 2; i < names.length; i++) {
                    var colName = names[i];
                    var sumValue = $(this).getCol(colName, false, 'sum');
                    footer += "\""+colName + "\":" + sumValue + ",";
                }
                footer += "\"小区\":\"合计\"}"
                //console.log(JSON.parse(footer));
                //SONObject jo = new JSONObject(new String(footer));

                $(this).footerData('set', JSON.parse(footer));

                //$(this).footerData('set', {colName: sumValue});
                //$("#jqGrid").jqGrid("footerData", "set", {小区:"合计"});

/*
                $(function(){
                    $(window).resize(function(){
                        $("#jqGrid").setGridHeight($(window).height()-100);
                    });
                });

 */
            }
        });

        //for (var i = 0; i <= jqdata.length; i++) {
            //jQuery("#jqGrid").jqGrid('addRowData', i + 1, jqdata[i]);
        //}
    });
});

$(function(){
    $(window).resize(function(){
        $("#jqGrid").setGridHeight($(window).height()-100);
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tDataErr: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tDataErr = {};
		},
		update: function (event) {
			var deviceId = getSelectedRow();
			if(deviceId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(deviceId)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.tDataErr.deviceId == null ? "water/tdataerr/save" : "water/tdataerr/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tDataErr),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var deviceIds = getSelectedRows();
			if(deviceIds == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "water/tdataerr/delete",
                        contentType: "application/json",
                        data: JSON.stringify(deviceIds),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(deviceId){
			$.get(baseURL + "water/tdataerr/info/"+deviceId, function(r){
                vm.tDataErr = r.tDataErr;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});